//-----------------------------------------------------------------------
// <copyright file="USERService.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------	

using System;
using System.Collections;
using MyFramework;
using MIMS.WRS.Entities;
using MIMS.WRS.Data;
    
namespace MIMS.WRS.Business
{   
	/// <summary>
	/// 
	/// </summary> 
    public class USERService
    {		
		public static int Login(string userName, string pwd) 
		{
			USER user = GetUSER(userName);
			if (user == null)
				throw new ApplicationException("Invalid user name or password");
			if (user.PASSWORD.Equals(pwd) == false)
				throw new ApplicationException("Invalid user name or password");
			return user.US_ID_PK;
		}

		public static void UpdatePassword(int userId, string newPwd) 
		{
			USER user = GetUSER(userId);
			user.PASSWORD = newPwd;
			UpdateUSER(user);
		}

		public static string GetOldPassword(int userId) 
		{
			return GetUSER(userId).PASSWORD;
		}

		public static USERCollection GetCustomerList()
		{            
			try
			{
				USERDAO userDAO = new USERDAO();
				return userDAO.GetCustomerList();
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetUSERsException, ex);
			}
		}    

		public static USERCollection GetUnreservedCustomerList()
		{            
			try
			{
				USERDAO userDAO = new USERDAO();
				return userDAO.GetUnreservedCustomerList();
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetUSERsException, ex);
			}
		}  

		public static USERCollection GetNonAdminUSERs(USERColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
		{            
			try
			{
				USERDAO userDAO = new USERDAO();
				return userDAO.GetNonAdminUSERs(orderBy, orderDirection, page, pageSize, out totalRecords);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetUSERsException, ex);
			}
		} 

		public static USER GetUSER(string loginName)
		{            
			try
			{
				USERDAO userDAO = new USERDAO();
				return userDAO.GetUSER(loginName);                
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetUSERException, ex);
			}
		}  

        #region USER

        public static void CreateUSER(USER user)
        {            
            try
            {
                USERDAO userDAO = new USERDAO();
                userDAO.CreateUSER(user);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessCreateUSERException, ex);
            }
        }        

        public static void UpdateUSER(USER user)
        {            
            try
            {
                USERDAO userDAO = new USERDAO();
                userDAO.UpdateUSER(user);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessUpdateUSERException, ex);
            }
        }        

        public static void DeleteUSER(int us_id_pk)
        {            
            try
            {
                USERDAO userDAO = new USERDAO();
                userDAO.DeleteUSER(us_id_pk);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessDeleteUSERException, ex);
            }
        }
        
        public static USER GetUSER(int us_id_pk)
        {            
            try
            {
                USERDAO userDAO = new USERDAO();
                return userDAO.GetUSER(us_id_pk);                
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetUSERException, ex);
            }
        }                

        public static USERCollection GetUSERs(USERColumns orderBy, OrderDirection orderDirection)
        {            
            try
            {
                USERDAO userDAO = new USERDAO();
                return userDAO.GetUSERs(orderBy, orderDirection);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetUSERsException, ex);
            }
        }        

        public static USERCollection GetUSERs(USERColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                USERDAO userDAO = new USERDAO();
                return userDAO.GetUSERs(orderBy, orderDirection, page, pageSize, out totalRecords);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetUSERsException, ex);
            }
        }        

        #endregion
    }
}